﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Windows.Resources;
using System.ComponentModel;
using VIBlend.WPF.Controls;
using System.Reflection;

namespace AnonsaiWPF.CSharp.DataGridViewDemos
{
    [Category("VIBlend.WPF.Examples")]
    public partial class GridLinesDemo : Window, IWindow
    {
        #region IWindow Members

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public Grid Root
        {
            get
            {
                return this.LayoutRoot;
            }
        }
        #endregion

        public void SetTheme()
        {
            dataGrid.FontFamily = new FontFamily("Tahoma");
        }
  
        public GridLinesDemo()
        {
            InitializeComponent();

            PrepareGridData(); 
            dataGrid.ItemsSource = listSaleRecord;
            dataGrid.ColumnsHierarchy.AllowDragDrop = false;
            dataGrid.ColumnsHierarchy.AllowResize = false;
            dataGrid.AllowContextMenuSorting = true;
        }

        #region Sample Data

        #region Data
        string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Michael",
                    "Guylène"
                };

        string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Saavedra",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Peterson",
                    "Winkler",
                    "Bein",
                    "Petersen",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Nodier"
                };

        static string[,] productNames = new string[,] 
                {
                    {"Black Tea", "1.95"},
                    {"Green Tea", "1.95"},
                    {"Caffè Espresso", "1.45"},
                    {"Doubleshot Espresso", "1.75"},
                    {"Caffè Latte", "2.25"},
                    {"White Chocolate Mocha", "2.35"},
                    {"Caramel Latte", "2.35"},
                    {"Caffè Americano", "1.65"},
                    {"Cappuccino", "2.10"},
                    {"Espresso Truffle", "2.45"},
                    {"Espresso con Panna", "1.81"},
                    {"Peppermint Mocha Twist", "1.99"}
                };

        public class EmployeeSaleRecord
        {
            public EmployeeSaleRecord(string Name, DateTime Date, int ProductId, int Quantity)
            {
                this._name = Name;
                this._date = Date;
                this._productid = ProductId;
                this._quantity = Quantity;
            }

            #region Private Members
            private string _name;
            private DateTime _date;
            private int _productid;
            private int _quantity;
            #endregion

            #region Properties

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            public DateTime Date
            {
                get { return _date; }
                set { _date = value; }
            }

            public string Product
            {
                get { return productNames[_productid, 0]; }
            }

            public int Quantity
            {
                get { return _quantity; }
                set { _quantity = value; }
            }

            public double UnitPrice
            {
                get
                {
                    double unitPrice = double.Parse(productNames[_productid, 1]);
                    return unitPrice;
                }
            }

            public double SalesAmount
            {
                get { return (double)_quantity * UnitPrice; }
            }


            #endregion
        }


        #endregion

        public List<EmployeeSaleRecord> listSaleRecord = new List<EmployeeSaleRecord>();

        private void PrepareGridData()
        {
            const int rowsToLoad = 1000;
            listSaleRecord.Clear();

            Random rand = new Random();

            for (int i = 0; i < rowsToLoad; i++)
            {
                int productId = rand.Next(0, productNames.Length / 2 - 1);
                int quantity = rand.Next(1, 5);
                listSaleRecord.Add(new EmployeeSaleRecord(
                    string.Format("{0} {1}",
                    firstNames[rand.Next(0, firstNames.Length - 1)],
                    lastNames[rand.Next(0, lastNames.Length - 1)]),
                    DateTime.Now.AddDays(-rand.Next(10, 100)),
                    productId,
                    quantity)
                    );
            }
        }

        #endregion

        // Custom class implements the IValueConverter interface.
        public class ProductIdToNameConverter : IValueConverter
        {
            #region IValueConverter Members

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                if ((int)value >= productNames.Length / 2)
                    return "";

                return productNames[(int)value, 0];
            }

            public object ConvertBack(object value, Type targetType,
                object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }

            #endregion
        }

        // Display all lines.
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid == null)
                return;

            this.dataGrid.GridLinesDisplayMode = GridLinesDisplayMode.DISPLAY_ALL;
            this.dataGrid.Refresh();
        }

        // Display only column lines.
        private void RadioButton_Checked_1(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid == null)
                return;

            this.dataGrid.GridLinesDisplayMode = GridLinesDisplayMode.DISPLAY_COLUMN_LINES;
            this.dataGrid.Refresh();
        }

        // Display only row lines.
        private void RadioButton_Checked_2(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid == null)
                return;
            
            this.dataGrid.GridLinesDisplayMode = GridLinesDisplayMode.DISPLAY_ROW_LINES;
            this.dataGrid.Refresh();
            this.dataGrid.InvalidateVisual();
        }

        // Hide all lines.
        private void RadioButton_Checked_3(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid == null)
                return;
            
            this.dataGrid.GridLinesDisplayMode = GridLinesDisplayMode.DISPLAY_NONE;
            this.dataGrid.Refresh();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.listSaleRecord.Clear();
            this.listSaleRecord = null;
            base.OnClosing(e);
            this.dataGrid.Dispose();
            this.dataGrid = null;
        }

    } // class CellEditing


}
